import common from './public.js'
$(function () {
	function paging(pageNow, pageSize, fuzzyQuery){
		window.location.href = common.baseUrl + "/department/list.do?pageNow=" + pageNow + "&pageSize=" + pageSize + "&fuzzyQuery=" + fuzzyQuery;
	}
	$("#department-search").keydown(function(event){
		event = event || window.event
		if(event.keyCode == 13){
			paging(1, $("#department-set-page-size option:selected").val(), $("#department-search").val())
		}
	})
	$("#department-search-icon").click(function(){
		paging(1, $("#department-set-page-size option:selected").val(), $("#department-search").val())
	})
	$("#department-set-page-size").change(function (){
		paging(1, $("#department-set-page-size option:selected").val(), $("#department-search").val())
	})
	$("#department-btn-batch-deletion").click(function(){
		var ids = []
		$('input[name="departmentId"]:checked').each(function(index, element) {
			//追加到数组中
			ids.push($(this).val());
		});
		common.post("/department/deleted.do", {ids: ids.join(",")}, function(res){
			if(res.code === 200){
				 common.promptSuccess(res.msg)
				 paging(1, $("#department-set-page-size option:selected").val(), $("#department-search").val())
				 // window.location = common.baseUrl + "/oneself.do"
			}else{
				 common.promptError(res.msg)
			}
		})
	})
	$("#department-pre-page").click(function(){
		paging(parseInt($("#department-page-now").text()) - 1, $("#department-set-page-size option:selected").val(), $("#department-search").val())
	})
	$("#department-next-page").click(function(){
		paging(parseInt($("#department-page-now").text()) + 1, $("#department-set-page-size option:selected").val(), $("#department-search").val())
	})
	/* $("#department-btn-add").click(function(){
		common.get("/department/getall.do",{},function (res){
			if(res.code === 200){
				 let data = res.data;
				 for (var i = 0; i < data.length; i++) {
					 var option = document.createElement("option")
					 option.setAttribute("value", data[i].id)
					 option.text = data[i].name
				 	$("#department-add-department").append(option)
				 }
			}else{
				 common.promptError(res.msg)
			}
		})
	}) */
	$("#department-add-submit-btn").click(function (){
		common.post("/department/add.do",{name: $("#department-add-name").val()},function (res){
			if(res.code === 200){
				 common.promptSuccess(res.msg)
				paging(1, $("#department-set-page-size option:selected").val(), $("#department-search").val())
			}else{
				 common.promptError(res.msg)
			}
		})
	})
})